const db = require("../config/config");
const jsonWebToken = require("jsonwebtoken");
const SECRET_KEY = "kite1874";
module.exports = {
    getAllData(req, res) {
        db.dbMysql(`SELECT * FROM deduction`, null, function (err, data) {
            console.log(data);
            data.forEach(item => {
                item.key = item.deductionId
            });
            const data1 = data
            res.json({
                code: 200,
                msg: '查询成功',
                data: data1
            })
        })
    },

    getAppointData(req, res) {
        console.log(req.body);
        db.dbMysql(`SELECT * FROM deduction WHERE deductionTitle like "%${req.body.deductionTitle}%" AND productGrounding=${req.body.productGrounding}`, null, function (err, data) {
            if (!err) {
                console.log(data.length);
                if (data.length === 0) {
                    res.json({
                        code: 200,
                        msg: '查询无数据',
                    })
                } else {
                    data.forEach(item => {
                        item.key = item.deductionId
                    });
                    const data1 = data
                    res.json({
                        code: 200,
                        msg: '查询成功',
                        data: data1
                    })
                }
            } else {
                res.json({
                    code: 500,
                    msg: "获取商品失败，请稍后重试",
                });
            }
        })
    },

    addData(req, res) {
        console.log(req.body);
        const reqData = req.body
        db.dbMysql(`INSERT INTO deduction (deductionId,deductionTitle,deductionStandard,deductionType,addTime,productGrounding) VALUES (?,?,?,?,?,?)`
            , [null, reqData.deductionTitle, reqData.deductionStandard, reqData.deductionType, reqData.addTime, reqData.productGrounding]
            , function (err, data) {
                if (data) {
                    res.json({
                        code: 200,
                        data: data,
                        msg: '添加成功',
                    })
                } else {
                    res.send({
                        code: 500,
                        msg: "服务器错误",
                    });
                }
            })
    },

    delData(req, res) {
        console.log(req.body);
        db.dbMysql(`DELETE FROM deduction WHERE deductionId = ${req.body.id}`, null, function (err, data) {
            if (data) {
                res.json({
                    code: 200,
                    msg: '删除成功'
                })
            } else {
                res.send({
                    code: 500,
                    msg: '删除失败'
                })
            }
        })
    },

    editData(req, res) {
        console.log(req.body);
        const reqData = req.body
        db.dbMysql(`UPDATE deduction SET deductionTitle='${reqData.deductionTitle}',deductionStandard='${reqData.deductionStandard}',deductionType='${reqData.deductionType}',addTime='${reqData.addTime}',productGrounding=${reqData.productGrounding} WHERE deductionId = ${reqData.deductionId}`
        , null
        , function (err, data) {
            console.log(err);
            if (!err) {
                res.json({
                    code: 200,
                    data: data,
                    msg: '修改成功',
                })
            } else {
                res.send({
                    code: 500,
                    msg: "服务器错误",
                });
            }
        })
    }
}